//
//  ZLPullMenuItem.m
//  ZLPullMenu
//
//  Created by 科大 on 2024/5/29.
//

#import "ZLPullMenuItem.h"

@implementation ZLPullMenuOption
+ (instancetype)optionWithTitle:(NSString *)optionTitle selected:(BOOL)selected {
    return [self optionWithTitle:optionTitle selected:selected config:[ZLPullMenuOptionpConfig defaultConfig]];
}
+ (instancetype)optionWithTitle:(NSString *)optionTitle selected:(BOOL)selected config:(ZLPullMenuOptionpConfig *)config {
    ZLPullMenuOption *option = [[ZLPullMenuOption alloc] init];
    option.optionTitle = optionTitle;
    option.selected = selected;
    option.config = config;
    return option;
}
@end

@implementation ZLPullMenuGroup
+ (instancetype)groupWithTitle:(NSString *)groupTitle options:(NSArray<ZLPullMenuOption *> *)options {
    return [self groupWithTitle:groupTitle selecteds:NO mustSelect:NO options:options];
}
+ (instancetype)groupWithTitle:(NSString *)groupTitle selecteds:(BOOL)selecteds mustSelect:(BOOL)mustSelect options:(NSArray<ZLPullMenuOption *> *)options {
    return [self groupWithTitle:groupTitle selecteds:selecteds mustSelect:mustSelect options:options config:[ZLPullMenuGroupConfig defaultConfig]];
}
+ (instancetype)groupWithTitle:(NSString *)groupTitle selecteds:(BOOL)selecteds mustSelect:(BOOL)mustSelect options:(NSArray<ZLPullMenuOption *> *)options config:(ZLPullMenuGroupConfig *)config {
    if (mustSelect) {
        NSInteger selectedCount = 0;
        for (ZLPullMenuOption *op in options) {
            if (op.selected) {
                selectedCount++;
            }
        }
        if (selectedCount == 0 && options.count > 0) {
            options.firstObject.selected = YES;
        }
    }
    ZLPullMenuGroup *group = [[ZLPullMenuGroup alloc] init];
    group.groupTitle = groupTitle;
    group.options = options;
    group.selecteds = selecteds;
    group.mustSelect = mustSelect;
    group.config = config;
    return group;
}
@end

@implementation ZLPullMenuItem
+ (instancetype)itemWithTitle:(NSString *)menuTitle groups:(NSArray<ZLPullMenuGroup *> *)groups {
    return [self itemWithTitle:menuTitle groups:groups config:[ZLPullMenuConfig defaultConfig]];
}
+ (instancetype)itemWithTitle:(NSString *)menuTitle groups:(NSArray<ZLPullMenuGroup *> *)groups config:(ZLPullMenuConfig *)config {
    ZLPullMenuItem *item = [[ZLPullMenuItem alloc] init];
    item.menuTitle = menuTitle;
    item.groups = groups;
    item.config = config;
    return item;
}
@end
